package de.davvid.contracts;

import java.io.*;
import javax.microedition.rms.*;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

public class ContractDatabase implements RecordComparator {

	private final static String recordStoreName="contracts";
	private final Vector contracts;

	public ContractDatabase() {
		contracts = new Vector();
		loadContracts();
	}

	private void loadContracts() {
		contracts.removeAllElements();
		try {
			RecordStore rs = null;
			try {
				rs = RecordStore.openRecordStore(recordStoreName, true);
				RecordEnumeration re = rs.enumerateRecords(null, this, true);
				while(re.hasNextElement()) {
					try {
						int id = re.nextRecordId();
						ByteArrayInputStream bais = new ByteArrayInputStream(rs.getRecord(id));
						DataInputStream recStr = new DataInputStream(bais);

						Date beginDate = new Date(recStr.readLong());
						Date endDate = new Date(recStr.readLong());
						int hours = recStr.readInt();

						try {	
							contracts.addElement(new Contract(beginDate,endDate,hours,id));//,new Integer(id));
						} catch (IllegalArgumentException e) {
							rs.deleteRecord(id); // delete invalid Record
						}

						recStr.close();
						bais.close();

					} catch(IOException e) {}
				}

			} catch(InvalidRecordIDException e) {

			} finally {
				if(rs != null) rs.closeRecordStore();
			}
		} catch(RecordStoreException e) {

		}
	}	

	public boolean addContract(Contract con) {
		if(con==null) return false;

		try {
			RecordStore rs = null;
			try {
				rs = RecordStore.openRecordStore(recordStoreName, true);
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				DataOutputStream outputStream = new DataOutputStream(baos);			

				outputStream.writeLong(con.getBeginDate().getTime());
				outputStream.writeLong(con.getEndDate().getTime());
				outputStream.writeInt(con.getHours());
				outputStream.flush();
				byte[] b = baos.toByteArray();

				outputStream.close();
				baos.close();

				rs.addRecord(b, 0, b.length);

			} catch (IOException e) {
				return false;
			} finally {
				if (rs != null) rs.closeRecordStore();
			}
		} catch (RecordStoreException e) {
			return false;
		}

		loadContracts();
		return true;
	}

	public Enumeration getContracts() {
		return contracts.elements();
	}
	
	public int size() {
		return contracts.size();
	}

	public boolean deleteContract(Contract con) {
		if (con==null)  return false;
		
		try {
			RecordStore rs = null;
			try {
				rs = RecordStore.openRecordStore(recordStoreName, true);
				rs.deleteRecord(con.getRecId());
			} finally {
				if (rs != null) rs.closeRecordStore();
			}
		} catch (RecordStoreException e) {
			return false;
		}

		loadContracts();
		return true;
	}

	public int compare(byte[] rec1, byte[] rec2) {
		long beginDate1=0, beginDate2=0;

		// Read data
		try {
			// Record 1
			ByteArrayInputStream bias1 = new ByteArrayInputStream(rec1);
			DataInputStream r1 = new DataInputStream(bias1);
			beginDate1 = r1.readLong();
			r1.close();
			bias1.close();

			// Record 2
			ByteArrayInputStream bias2 = new ByteArrayInputStream(rec2);
			DataInputStream r2 = new DataInputStream(bias2);
			beginDate2 = r2.readLong();
			r2.close();
			bias2.close();

		} catch (IOException e) {}

		// Analyse data
		if (beginDate1 > beginDate2) {
			return RecordComparator.PRECEDES;
		}
		else if (beginDate1 < beginDate2) {
			return RecordComparator.FOLLOWS;
		}
		else {
			return RecordComparator.EQUIVALENT;
		}
	}

}
